1 /*
2   D bindings for CUDA.
3   Authors:    Prasun Anand
4   Copyright:  Copyright (c) 2017, Prasun Anand. All rights reserved.
5   License:    BSD 3-Clause License
6 */
7 
8 module cuda_d.curand;
9 
10 import cuda_d.cublas_api;
11 
12 extern (C):
13 
14 enum curandStatus
15 {
16     CURAND_STATUS_SUCCESS = 0, ///< No errors
17     CURAND_STATUS_VERSION_MISMATCH = 100, ///< Header file and linked library version do not match
18     CURAND_STATUS_NOT_INITIALIZED = 101, ///< Generator not initialized
19     CURAND_STATUS_ALLOCATION_FAILED = 102, ///< Memory allocation failed
20     CURAND_STATUS_TYPE_ERROR = 103, ///< Generator is wrong type
21     CURAND_STATUS_OUT_OF_RANGE = 104, ///< Argument out of range
22     CURAND_STATUS_LENGTH_NOT_MULTIPLE = 105, ///< Length requested is not a multple of dimension
23     CURAND_STATUS_DOUBLE_PRECISION_REQUIRED = 106, ///< GPU does not have double precision required by MRG32k3a
24     CURAND_STATUS_LAUNCH_FAILURE = 201, ///< Kernel launch failure
25     CURAND_STATUS_PREEXISTING_FAILURE = 202, ///< Preexisting failure on library entry
26     CURAND_STATUS_INITIALIZATION_FAILED = 203, ///< Initialization of CUDA failed
27     CURAND_STATUS_ARCH_MISMATCH = 204, ///< Architecture mismatch, GPU does not support requested feature
28     CURAND_STATUS_INTERNAL_ERROR = 999 ///< Internal library error
29 }
30 
31 alias curandStatus_t = curandStatus;
32 
33 enum curandRngType
34 {
35     CURAND_RNG_TEST = 0,
36     CURAND_RNG_PSEUDO_DEFAULT = 100, ///< Default pseudorandom generator
37     CURAND_RNG_PSEUDO_XORWOW = 101, ///< XORWOW pseudorandom generator
38     CURAND_RNG_PSEUDO_MRG32K3A = 121, ///< MRG32k3a pseudorandom generator
39     CURAND_RNG_PSEUDO_MTGP32 = 141, ///< Mersenne Twister MTGP32 pseudorandom generator
40     CURAND_RNG_PSEUDO_MT19937 = 142, ///< Mersenne Twister MT19937 pseudorandom generator
41     CURAND_RNG_PSEUDO_PHILOX4_32_10 = 161, ///< PHILOX-4x32-10 pseudorandom generator
42     CURAND_RNG_QUASI_DEFAULT = 200, ///< Default quasirandom generator
43     CURAND_RNG_QUASI_SOBOL32 = 201, ///< Sobol32 quasirandom generator
44     CURAND_RNG_QUASI_SCRAMBLED_SOBOL32 = 202, ///< Scrambled Sobol32 quasirandom generator
45     CURAND_RNG_QUASI_SOBOL64 = 203, ///< Sobol64 quasirandom generator
46     CURAND_RNG_QUASI_SCRAMBLED_SOBOL64 = 204 ///< Scrambled Sobol64 quasirandom generator
47 }
48 
49 alias curandRngType_t = curandRngType;
50 
51 enum curandOrdering
52 {
53     CURAND_ORDERING_PSEUDO_BEST = 100, ///< Best ordering for pseudorandom results
54     CURAND_ORDERING_PSEUDO_DEFAULT = 101, ///< Specific default 4096 thread sequence for pseudorandom results
55     CURAND_ORDERING_PSEUDO_SEEDED = 102, ///< Specific seeding pattern for fast lower quality pseudorandom results
56     CURAND_ORDERING_QUASI_DEFAULT = 201 ///< Specific n-dimensional ordering for quasirandom results
57 }
58 
59 /*
60  * CURAND ordering of results in memory
61  */
62 /** \cond UNHIDE_TYPEDEFS */
63 alias curandOrdering_t = curandOrdering;
64 
65 enum curandDirectionVectorSet
66 {
67     CURAND_DIRECTION_VECTORS_32_JOEKUO6 = 101, ///< Specific set of 32-bit direction vectors generated from polynomials recommended by S. Joe and F. Y. Kuo, for up to 20,000 dimensions
68     CURAND_SCRAMBLED_DIRECTION_VECTORS_32_JOEKUO6 = 102, ///< Specific set of 32-bit direction vectors generated from polynomials recommended by S. Joe and F. Y. Kuo, for up to 20,000 dimensions, and scrambled
69     CURAND_DIRECTION_VECTORS_64_JOEKUO6 = 103, ///< Specific set of 64-bit direction vectors generated from polynomials recommended by S. Joe and F. Y. Kuo, for up to 20,000 dimensions
70     CURAND_SCRAMBLED_DIRECTION_VECTORS_64_JOEKUO6 = 104 ///< Specific set of 64-bit direction vectors generated from polynomials recommended by S. Joe and F. Y. Kuo, for up to 20,000 dimensions, and scrambled
71 }
72 
73 alias curandDirectionVectorSet_t = curandDirectionVectorSet;
74 
75 alias curandDirectionVectors32_t = uint[32];
76 
77 alias curandDirectionVectors64_t = ulong[64];
78 
79 struct curandGenerator_st;
80 
81 alias curandGenerator_t = curandGenerator_st*;
82 
83 alias curandDistribution_st = double;
84 alias curandDistribution_t = double*;
85 struct curandDistributionShift_st;
86 alias curandDistributionShift_t = curandDistributionShift_st*;
87 
88 struct curandDistributionM2Shift_st;
89 alias curandDistributionM2Shift_t = curandDistributionM2Shift_st*;
90 struct curandHistogramM2_st;
91 alias curandHistogramM2_t = curandHistogramM2_st*;
92 alias curandHistogramM2K_st = uint;
93 alias curandHistogramM2K_t = uint*;
94 alias curandHistogramM2V_st = double;
95 alias curandHistogramM2V_t = double*;
96 
97 struct curandDiscreteDistribution_st;
98 alias curandDiscreteDistribution_t = curandDiscreteDistribution_st*;
99 
100 enum curandMethod
101 {
102     CURAND_CHOOSE_BEST = 0, // choose best depends on args
103     CURAND_ITR = 1,
104     CURAND_KNUTH = 2,
105     CURAND_HITR = 3,
106     CURAND_M1 = 4,
107     CURAND_M2 = 5,
108     CURAND_BINARY_SEARCH = 6,
109     CURAND_DISCRETE_GAUSS = 7,
110     CURAND_REJECTION = 8,
111     CURAND_DEVICE_API = 9,
112     CURAND_FAST_REJECTION = 10,
113     CURAND_3RD = 11,
114     CURAND_DEFINITION = 12,
115     CURAND_POISSON = 13
116 }
117 
118 alias curandMethod_t = curandMethod;
119 
120 curandStatus_t curandCreateGenerator (
121     curandGenerator_t* generator,
122     curandRngType_t rng_type);
123 
124 curandStatus_t curandCreateGeneratorHost (
125     curandGenerator_t* generator,
126     curandRngType_t rng_type);
127 
128 
129 curandStatus_t curandDestroyGenerator (curandGenerator_t generator);
130 
131 
132 curandStatus_t curandGetVersion (int* version_);
133 
134 curandStatus_t curandSetStream (
135     curandGenerator_t generator,
136     cudaStream_t stream);
137 
138 curandStatus_t curandSetPseudoRandomGeneratorSeed (
139     curandGenerator_t generator,
140     ulong seed);
141 
142 curandStatus_t curandSetGeneratorOffset (
143     curandGenerator_t generator,
144     ulong offset);
145 
146 curandStatus_t curandSetGeneratorOrdering (
147     curandGenerator_t generator,
148     curandOrdering_t order);
149 
150 curandStatus_t curandSetQuasiRandomGeneratorDimensions (
151     curandGenerator_t generator,
152     uint num_dimensions);
153 
154 curandStatus_t curandGenerate (
155     curandGenerator_t generator,
156     uint* outputPtr,
157     size_t num);
158 
159 curandStatus_t curandGenerateLongLong (
160     curandGenerator_t generator,
161     ulong* outputPtr,
162     size_t num);
163 
164 curandStatus_t curandGenerateUniform (
165     curandGenerator_t generator,
166     float* outputPtr,
167     size_t num);
168 
169 curandStatus_t curandGenerateUniformDouble (
170     curandGenerator_t generator,
171     double* outputPtr,
172     size_t num);
173 
174 curandStatus_t curandGenerateNormal (
175     curandGenerator_t generator,
176     float* outputPtr,
177     size_t n,
178     float mean,
179     float stddev);
180 
181 curandStatus_t curandGenerateNormalDouble (
182     curandGenerator_t generator,
183     double* outputPtr,
184     size_t n,
185     double mean,
186     double stddev);
187 
188 curandStatus_t curandGenerateLogNormal (
189     curandGenerator_t generator,
190     float* outputPtr,
191     size_t n,
192     float mean,
193     float stddev);
194 
195 curandStatus_t curandGenerateLogNormalDouble (
196     curandGenerator_t generator,
197     double* outputPtr,
198     size_t n,
199     double mean,
200     double stddev);
201 
202 curandStatus_t curandCreatePoissonDistribution (
203     double lambda,
204     curandDiscreteDistribution_t* discrete_distribution);
205 
206 curandStatus_t curandDestroyDistribution (
207     curandDiscreteDistribution_t discrete_distribution);
208 
209 curandStatus_t curandGeneratePoisson (
210     curandGenerator_t generator,
211     uint* outputPtr,
212     size_t n,
213     double lambda);
214 
215 curandStatus_t curandGeneratePoissonMethod (
216     curandGenerator_t generator,
217     uint* outputPtr,
218     size_t n,
219     double lambda,
220     curandMethod_t method);
221 
222 curandStatus_t curandGenerateSeeds (curandGenerator_t generator);
223 
224 curandStatus_t curandGetDirectionVectors32 (
225     curandDirectionVectors32_t** vectors,
226     curandDirectionVectorSet_t set);
227 
228 curandStatus_t curandGetScrambleConstants32 (uint** constants);
229 
230 curandStatus_t curandGetDirectionVectors64 (
231     curandDirectionVectors64_t** vectors,
232     curandDirectionVectorSet_t set);
233 
234 curandStatus_t curandGetScrambleConstants64 (ulong** constants);